package org.cuckoos.android;

import java.util.ArrayList;
import java.util.List;

import org.cuckoos.android.command.EntryCommand;
import org.cuckoos.android.domain.Constant;
import org.cuckoos.android.domain.User;
import org.cuckoos.android.ui.AppActivity;

import android.widget.ArrayAdapter;

public class ContactBook {
	public ContactBook(AppActivity activity){
		this.activity  = activity;
	}
	private AppActivity activity;
	/**
	 * 保存了联系人列表,User实现了equals,保证唯一性
	 */
	private List<User> contactBook = new ArrayList<User>();
	/**
	 * 添加用户
	 * @param user
	 * @return 返回ip地址
	 */
	public String addUser(User user){
		if(contactBook.contains(user))
			return user.getIp();    //通信录中有此用户
		contactBook.add(user);
		activity.runOnUiThread(new Runnable() {

			@Override
			public void run() {
				activity.getContactBookView().setAdapter(new ArrayAdapter<User>(activity,
						android.R.layout.simple_list_item_1, contactBook));
			}
		});
		return user.getIp();
	}
	
	public String removeUser(User user){
		return null;
	}
	
	public String modifyUser(User user){
		return null;
	}
	/**
	 * 删除所有用户
	 */
	public void removeAll(){
		contactBook.clear();
	}
	/**
	 * 更新所有用户
	 */
	public void updateAll(){
		removeAll();
		EntryCommand ec = new EntryCommand(SocketService.getDefault().getSocket(),Constant.getOverLord());
		ec.execute();
	}
}
